package com.base.utils;

import android.app.Activity;
import android.content.Context;
import android.support.v4.content.ContextCompat;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;

/**
 * 获取图片工具类
 * Created by Android on 2017/3/6 0006.
 */

public class ImageUtil {

    /**
     * 获取静态图片并赋给view(图片可显示完全但并不会铺满控件)
     */
    public static void setFitBitmap(Context context, ImageView view, String url) {
        Glide.with(context)
                .load(url)
                .asBitmap()
                .fitCenter()
                .diskCacheStrategy(DiskCacheStrategy.ALL)
//                .placeholder(R.mipmap.ic_launcher)
//                .error(R.mipmap.ic_launcher)
                .into(view);
    }

    /**
     *获取静态图片并赋给view(图片可铺满控件但会显示不全)
     */
    public static void setCropBitmap(Context context, ImageView view, String url, int defIcon) {
        if (!StringUtil.isEmpty(url) && url.indexOf("//") == 0) {
            url = "http:" + url;
        }
        if (!((Activity)context).isFinishing()) {
            Glide.with(context)
                    .load(url)
                    .asBitmap()
                    .centerCrop()
                    .diskCacheStrategy(DiskCacheStrategy.ALL)
                    .placeholder(defIcon)
//                .error(R.mipmap.ic_launcher)
                    .into(view);
        }
    }

    /**
     * 获取动态图片并赋给view
     */
    public static void setGif(Context context, ImageView view, String url) {
        Glide.with(context)
                .load(url)
                .asGif()
                .centerCrop()
//                .placeholder(R.mipmap.ic_launcher)
//                .error(R.mipmap.ic_launcher)
                .into(view);
    }

    /**
     * 获取颜色
     * @param context
     * @param resId
     * @return
     */
    public static int getColor(Context context, int resId) {
        return ContextCompat.getColor(context, resId);
    }
}
